1
回答
android mediaplay
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

运行刚写的播放音频的应用程序 出现setDataSource failed.: status=0x80000000,SD卡中已导入音乐

public class MusicService extends Service implements OnPreparedListener{
    //Intent Action播放传递Action
    public static final String PLAY_ACTION="com.music.play";
    public static final String PAUSE_ACTION="com.music.pause";
    public static final String STOP_ACTION="com.music.stop";
    public static final String REW_QIAN="com.music.qian";
    public static final String REW_HOU="com.music.hou";
//实例化一个MediaPlayer
    private MediaPlayer mPlay=null;
    // 播放媒体库文件需要uri,contentUris类的withAppendedId方法
    Uri uri=ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 1);
    //onCreate方法 new MediaPlayer 因为在该Service没有销毁,无论调用多少次onCreate,
    //都是在一个Service上,oncreat()方法之执行一次
    @Override
    public void onCreate(){
       
        super.onCreate();
       
        mPlay=new MediaPlayer();
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
   @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    String action=intent.getAction();
    //根据不同的action进行不同的方法调用
    if(action.equals(MusicService.PLAY_ACTION)){
        play();
    }else if(action.equals(MusicService.PAUSE_ACTION)){
        pause();
    }else if(action.equals(MusicService.STOP_ACTION)){
        //stopSelf方法调用后,自动调用onDestroy 销毁服务
        stopSelf();
    }else if(action.equals(MusicService.REW_QIAN)){
        rew_qian();
    }else if(action.equals(MusicService.REW_HOU)){
        rew_hou();
    }
    return super.onStartCommand(intent, flags, startId);
}
   //异步运行
   private void play(){
       try {
           //加载歌曲
        mPlay.setDataSource(getApplicationContext(),uri);
         mPlay.setAudioStreamType(AudioManager.STREAM_MUSIC);
           mPlay.setOnPreparedListener(this);
           mPlay.prepare();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     
   }
   public void rew_qian(){
       mPlay.pause();
       int current=mPlay.getCurrentPosition();
       mPlay.seekTo(current-200);
       mPlay.start();
   }
   public void rew_hou(){
       mPlay.pause();
       int current=mPlay.getCurrentPosition();
       mPlay.seekTo(current+200);
       mPlay.start();
       }
private void pause(){
    if(mPlay.isPlaying()){
    mPlay.pause();
    }
}
@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    mPlay.release();
    mPlay=null;
}
public void onPrepared(MediaPlayer mp) {
    // TODO Auto-generated method stub
    mp.start();
}
}
-----------------------------------------------------------------------------------------

public class MediaActivity extends Activity {
//    MediaPlayer mMediaPlayer=null;
//    private String url="http://zhangmenshiting2.baidu.com/data2/music/10547672/10547672.mp3?xcode=dd40b34fad11a01fc723a795b676d483&mid=0.55987668790549";
    private ComponentName mName;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mName=new ComponentName(getApplicationContext(), MusicService.class);

    }
    public void playMusic(View view){

      Intent intent=new Intent(MusicService.PLAY_ACTION);
      intent.setComponent(mName);
      //启动Service
      startService(intent);
    }
    public void rewqin(View view){
        //Intent intent=new Intent(MusicService.REW_QIAN);
        //intent.setComponent(mName);
        //startService(intent);
    }
    public void rewhou(View view ){
        //Intent intent=new Intent(MusicService.REW_HOU);
        //intent.setComponent(mName);
        //startService(intent);
    }
  //上机作业完成暂停
    public void pauseMusic(View view){
         Intent intent=new Intent(MusicService.PAUSE_ACTION);
         intent.setComponent(mName);
         startService(intent);
    }
    //MediaPlayer【必须】手动释放
    //GC垃圾回收器不会立即回收资源
    //完成停止service
//    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
         Intent intent=new Intent(MusicService.STOP_ACTION);
         intent.setComponent(mName);
         startService(intent);
    }
}

举报
echocao
发帖于6年前 1回/1K+阅
顶部