溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android中如何播放音樂

發(fā)布時間:2021-06-28 17:08:31 來源:億速云 閱讀:154 作者:Leah 欄目:移動開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)Android中如何播放音樂,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

具體內(nèi)容如下

//媒體播放器
private MediaPlayer player;

public void onCreate() {
  File file=new File(Environment.getExternalStorageDirectory(),"a.mp3");
  player =new MediaPlayer();
  try {
   //設(shè)置播放源
   player.setDataSource(file.getAbsolutePath());
  } catch (Exception e) {
   e.printStackTrace();
  } 
  Log.d("fanfan", "onCreate");
  super.onCreate();
 }
public int onStartCommand(Intent intent, int flags, int startId) {
  
  try {
   //設(shè)置準(zhǔn)備資源監(jiān)聽器,當(dāng)資源準(zhǔn)備完畢,回調(diào)監(jiān)聽器的onPrepared函數(shù)
   player.setOnPreparedListener(new OnPreparedListener() {
    @Override
    //準(zhǔn)備資源準(zhǔn)備好了會調(diào)用這個
    public void onPrepared(MediaPlayer arg0) {
     //播放音樂
     player.start();
    }
   });
   
   //準(zhǔn)備資源,好來播放音樂
   //異步函數(shù),這個函數(shù)內(nèi)部會開啟一個子線程
   player.prepareAsync();
   
  } catch (Exception e) {
   e.printStackTrace();
  } 
  
  Log.d("fanfan", "onStartCommand");
  return super.onStartCommand(intent, flags, startId);
 }
public void onDestroy() {
    //結(jié)束音樂
    player.stop();
    //釋放資源,如果播放下一首的話,就用不著釋放資源
    player.release();
    Log.d("fanfan", "onDestroy");
    super.onDestroy();
  }

第一步,照樣找個類來繼承服務(wù)類

package fry;

import java.io.File;
import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

public class myService extends Service{

  //媒體播放器
  private MediaPlayer player;
  /**
   * 當(dāng)綁定這個服務(wù)的時候調(diào)用
   */
  @Override
  public IBinder onBind(Intent arg0) {
    Log.d("fanfan", "onBind");
    return null;
  }
  /**
   * service被創(chuàng)建后調(diào)用
   */
  @Override
  public void onCreate() {
    File file=new File(Environment.getExternalStorageDirectory(),"a.mp3");
    player =new MediaPlayer();
    try {
      //設(shè)置播放源
      player.setDataSource(file.getAbsolutePath());
    } catch (Exception e) {
      e.printStackTrace();
    } 
    Log.d("fanfan", "onCreate");
    super.onCreate();
  }
  
  /**
   * service被start后調(diào)用
   */
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    
    try {
      //設(shè)置準(zhǔn)備資源監(jiān)聽器,當(dāng)資源準(zhǔn)備完畢,回調(diào)監(jiān)聽器的onPrepared函數(shù)
      player.setOnPreparedListener(new OnPreparedListener() {
        @Override
        //準(zhǔn)備資源準(zhǔn)備好了會調(diào)用這個
        public void onPrepared(MediaPlayer arg0) {
          //播放音樂
          player.start();
        }
      });
      
      //準(zhǔn)備資源,好來播放音樂
      //異步函數(shù),這個函數(shù)內(nèi)部會開啟一個子線程
      player.prepareAsync();
      
    } catch (Exception e) {
      e.printStackTrace();
    } 
    
    Log.d("fanfan", "onStartCommand");
    return super.onStartCommand(intent, flags, startId);
  }
  
  /**
   * service被停止后調(diào)用
   */
  @Override
  public void onDestroy() {
    //結(jié)束音樂
    player.stop();
    //釋放資源,如果播放下一首的話,就用不著釋放資源
    player.release();
    Log.d("fanfan", "onDestroy");
    super.onDestroy();
  }

}

第二步,該配置的監(jiān)聽服務(wù)也是要配置的

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.playMusic"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
      android:name="fry.MainActivity"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name="fry.Activity01" android:exported="true"></activity>
    
    <service android:name="fry.myService">
      
    </service>
    
  </application>

</manifest>

第三步,播放或者結(jié)束音樂

package fry;

import com.example.playMusic.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class Activity01 extends Activity{
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity01);
  }
  
  public void onClick(View view){
    Intent intent=new Intent();
    intent.setClass(this, myService.class);
    switch(view.getId()){
    case R.id.btn_start://播放音樂,啟動服務(wù)
      startService(intent);
      break;
    case R.id.btn_stop://結(jié)束音樂,停止服務(wù)
      stopService(intent);
      break;
    }
  }
}

關(guān)于Android中如何播放音樂就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI