Expert: Stream Online Music Like Never Before With Huawei Audio Editor in Android
Overview
In this article, I will create a Music Player App along with the integration of HMS Audio Editor. It provides a new experience of listing music with special effects and much more.
HMS Audio Editor Kit Service Introduction
HMS Audio Editor provides a wide range of audio editing capabilities, including audio import, export, editing, extraction, and conversion.
Audio Editor Kit provides various APIs for editing audio which helps to create custom equaliser so that user can create their own equaliser.
SDK does not collect personal data but reports API call results to the BI server. The SDK uses HTTPS for encrypted data transmission. BI data is reported to sites in different areas based on users’ home locations. The BI server stores the data and protects data security.
Prerequisite
- AppGallery Account
- Android Studio 3.X
- SDK Platform 19 or later
- Gradle 4.6 or later
- HMS Core (APK) 5.0.0.300 or later
- Huawei Phone EMUI 5.0 or later
- Non-Huawei Phone Android 5.0 or later
App Gallery Integration process
- Sign In and Create or Choose a project on AppGallery Connect portal.
2. Navigate to Project settings and download the configuration file.
3. Navigate to General Information, and then provide Data Storage location.
App Development
- Create A New Project, choose Empty Activity > Next.
2. Configure Project Gradle.
3. Configure App Gradle.
4. Configure AndroidManifest.xml.
API Overview
- Set Audio Path:
2. transformAudioUseDefaultPath to convert audio and save converted audio to the default directory.
3. transformAudio to convert audio and save converted audio to a specified directory.
4. extractAudio to extract audio from video and save extracted audio to a specified directory.
Create Activity class with XML UI.
MainActivity:
This activity performs audio streaming realted operations
package com.huawei.hms.audiokitdemotest;import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.huawei.hms.api.bean.HwAudioPlayItem;
import com.huawei.hms.audiokit.player.callback.HwAudioConfigCallBack;
import com.huawei.hms.audiokit.player.manager.HwAudioManager;
import com.huawei.hms.audiokit.player.manager.HwAudioManagerFactory;
import com.huawei.hms.audiokit.player.manager.HwAudioPlayerConfig;
import com.huawei.hms.audiokit.player.manager.HwAudioPlayerManager;import java.util.ArrayList;
import java.util.List;import my_interface.OnItemClickListener;public class MainActivity extends AppCompatActivity implements OnItemClickListener, View.OnClickListener {
private static RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private static RecyclerView recyclerView;
private static ArrayList<DataModel> data;
// static View.OnClickListener myOnClickListener; private static final String TAG = "MainActivity"; private HwAudioManager mHwAudioManager; private HwAudioPlayerManager mHwAudioPlayerManager; // Listener
private OnItemClickListener onItemClickListener;
private LinearLayout layout_music_widget;
private ImageView play,pause,close; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init(MainActivity.this);
// myOnClickListener = new MyOnClickListener(this);
layout_music_widget = (LinearLayout) findViewById(R.id.layout_music_widget);
play = (ImageView) findViewById(R.id.play_pause);
pause = (ImageView) findViewById(R.id.pause);
close = (ImageView) findViewById(R.id.close);
play.setOnClickListener(this);
pause.setOnClickListener(this);
close.setOnClickListener(this);
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
// recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
data = new ArrayList<DataModel>();
for (int i = 0; i < MyData.nameArray.length; i++) {
data.add(new DataModel(
MyData.nameArray[i],
MyData.DurationArray[i],
MyData.YearArray[i],
MyData.BriefArray[i],
MyData.id_[i],
MyData.drawableArray[i]
));
} // adapter = new CustomAdapter(data);
adapter = new CustomAdapter(this,data, this); recyclerView.setAdapter(adapter);
} @Override
public void onItemClick(int pos) {
layout_music_widget.setVisibility(View.VISIBLE);
play.setVisibility(View.GONE);
pause.setVisibility(View.VISIBLE);
Toast.makeText(this, "Hello"+MyData.nameArray[pos], Toast.LENGTH_LONG).show();
if (mHwAudioPlayerManager != null) {
if(mHwAudioPlayerManager.isPlaying()){
mHwAudioPlayerManager.stop();
}
mHwAudioPlayerManager.play();
} } @Override
public void onClick(View v) {
switch (v.getId()){
case R.id.play_pause:
Toast.makeText(this, "Play", Toast.LENGTH_LONG).show();
play.setVisibility(View.GONE);
pause.setVisibility(View.VISIBLE);
if (mHwAudioPlayerManager != null) {
mHwAudioPlayerManager.play();
}
break;
case R.id.pause:
Toast.makeText(this, "pause", Toast.LENGTH_LONG).show();
play.setVisibility(View.VISIBLE);
pause.setVisibility(View.GONE);
if (mHwAudioPlayerManager != null) {
mHwAudioPlayerManager.pause();
}
break;
case R.id.close:
Toast.makeText(this, "Close", Toast.LENGTH_LONG).show();
layout_music_widget.setVisibility(View.GONE);
if (mHwAudioPlayerManager != null) {
mHwAudioPlayerManager.stop();
}
break; default:
break;
}
} /**
* init sdk
* @param context context
*/
@SuppressLint("StaticFieldLeak")
public void init(final Context context) {
Log.i(TAG, "init start");
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
HwAudioPlayerConfig hwAudioPlayerConfig = new HwAudioPlayerConfig(context);
HwAudioManagerFactory.createHwAudioManager(hwAudioPlayerConfig, new HwAudioConfigCallBack() {
@Override
public void onSuccess(HwAudioManager hwAudioManager) {
try {
Log.i(TAG, "createHwAudioManager onSuccess");
mHwAudioManager = hwAudioManager;
mHwAudioPlayerManager = hwAudioManager.getPlayerManager();
mHwAudioPlayerManager.playList(getOnlinePlaylist(), 0, 0);
} catch (Exception e) {
Log.e(TAG, "player init fail", e);
}
} @Override
public void onError(int errorCode) {
Log.e(TAG, "init err:" + errorCode);
}
});
return null;
}
}.execute();
} /**
* get online PlayList
*
* @return play list
*/
public List<HwAudioPlayItem> getOnlinePlaylist() {
List<HwAudioPlayItem> playItemList = new ArrayList<>();
HwAudioPlayItem audioPlayItem1 = new HwAudioPlayItem();
audioPlayItem1.setAudioId("1000");
audioPlayItem1.setSinger("Taoge");
audioPlayItem1.setOnlinePath("https://lfmusicservice.hwcloudtest.cn:18084/HMS/audio/Taoge-chengshilvren.mp3");
audioPlayItem1.setOnline(1);
audioPlayItem1.setAudioTitle( MyData.nameArray[0]);
playItemList.add(audioPlayItem1); HwAudioPlayItem audioPlayItem2 = new HwAudioPlayItem();
audioPlayItem2.setAudioId("1001");
audioPlayItem2.setSinger("Kailash Kher");
audioPlayItem2.setOnlinePath("https://lfmusicservice.hwcloudtest.cn:18084/HMS/audio/Taoge-dayu.mp3");
audioPlayItem2.setOnline(1);
audioPlayItem2.setAudioTitle( MyData.nameArray[1]);
playItemList.add(audioPlayItem2); HwAudioPlayItem audioPlayItem3 = new HwAudioPlayItem();
audioPlayItem3.setAudioId("1002");
audioPlayItem3.setSinger("Kailash Kher");
audioPlayItem3.setOnlinePath("https://lfmusicservice.hwcloudtest.cn:18084/HMS/audio/Taoge-wangge.mp3");
audioPlayItem3.setOnline(1);
audioPlayItem3.setAudioTitle( MyData.nameArray[2]);
playItemList.add(audioPlayItem3); HwAudioPlayItem playItemList4 = new HwAudioPlayItem();
playItemList4.setAudioId("1000");
playItemList4.setSinger("Kailash Kher");
playItemList4.setOnlinePath("https://lfmusicservice.hwcloudtest.cn:18084/HMS/audio/Taoge-chengshilvren.mp3");
playItemList4.setOnline(1);
playItemList4.setAudioTitle( MyData.nameArray[3]);
playItemList.add(playItemList4); HwAudioPlayItem audioPlayItem5 = new HwAudioPlayItem();
audioPlayItem5.setAudioId("1001");
audioPlayItem5.setSinger("Kailash Kher");
audioPlayItem5.setOnlinePath("https://lfmusicservice.hwcloudtest.cn:18084/HMS/audio/Taoge-dayu.mp3");
audioPlayItem5.setOnline(1);
audioPlayItem5.setAudioTitle( MyData.nameArray[4]);
playItemList.add(audioPlayItem5); HwAudioPlayItem audioPlayItem6 = new HwAudioPlayItem();
audioPlayItem6.setAudioId("1002");
audioPlayItem6.setSinger("Kailash Kher");
audioPlayItem6.setOnlinePath("https://lfmusicservice.hwcloudtest.cn:18084/HMS/audio/Taoge-wangge.mp3");
audioPlayItem6.setOnline(1);
audioPlayItem6.setAudioTitle( MyData.nameArray[5]);
playItemList.add(audioPlayItem6);
return playItemList;
}
}
activity_main.xml-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity1"
android:weightSum="2"
android:background="@drawable/bg_music" >
<ImageButton
android:id="@+id/more"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/description_text"
android:background="@drawable/expand_arrow"
android:clickable="true"
android:layout_gravity="left"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:visibility="gone" />
<ImageButton
android:id="@+id/less"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/description_text"
android:background="@drawable/expand_arrow"
android:clickable="true"
android:layout_gravity="left"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:visibility="visible"
android:rotation="90"
/>
<LinearLayout
android:id="@+id/artist_image_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
> <ImageView
android:id="@+id/image"
android:layout_width="250dp"
android:layout_height="250dp"
android:gravity="center"
android:src="@drawable/bahubali" />
</LinearLayout>
<LinearLayout
android:id="@+id/remain_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical"> <LinearLayout
android:id="@+id/contentview_more"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp" android:visibility="gone">
<TextView
android:id="@+id/songTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="5dp"
android:text="BahuBali"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"/>
<TextView
android:id="@+id/songArtist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="5dp"
android:textColor="@color/colorAccent"
android:text="K. J Yesudas"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"/>
<FrameLayout
android:id="@+id/pro_layout"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" >
</FrameLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"> <ImageView
android:id="@+id/prev"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="5dp"
android:layout_gravity="center"
android:src="@drawable/btn_playback_pre_normal" /> <ImageView
android:id="@+id/play_pause"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:layout_gravity="center"
android:src="@drawable/btn_playback_play_normal" /> <ImageView
android:id="@+id/next"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="5dp"
android:layout_gravity="center"
android:src="@drawable/btn_playback_next_normal" /> </LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/contentview_less"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/play_pause_less_mode"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:src="@drawable/btn_playback_play_normal" />
<TextView
android:id="@+id/plalist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_marginLeft="10dp"
android:layout_marginBottom="5dp"
android:text="Playlist"
android:textColor="@color/colorAccent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
App Build Result
Tips and Tricks
- Audio Editor Kit is supported on Huawei phones running EMUI 5.0 or later and non-Huawei phones running Android 5.0 or later.
- All APIs provided by the Audio Editor SDK are free of charge.
- Audio Editor Kit supports all audio formats during audio import. It supports exporting audio into MP3, WAV, AAC, or M4A format.
Conclusion
In this article, we have learned how to integrate HMS Audio Editor in Music Player android application. Audio Kit provides an excellent experience in Audio playback. It allows developers to quickly build their own local or online playback applications. It can provide a better hearing effects based on the multiple audio effects capabilities.
Thanks for reading this article. Be sure to like and comment to this article, if you found it helpful. It means a lot to me.
References
HMS Audio Editor Docs:
https://developer.huawei.com/consumer/en/doc/development/Media-Guides/introduction-0000001153026881