2014-05-24 1 views
1

내 코드에서 Uri.parse가 MediaPlayer가 특수 문자 파일 이름에 "#"등이 있습니다. 이 문제를 해결하는 방법을 알 수 없습니다. 내 파일 이름에 특수 문자를 사용할 수 있기를 원합니다.Android MediaPlayer가 Uri.parse (경로)를 사용하여 파일 이름에 '#'문자가 포함 된 파일을 재생할 수 없음

public void playAudio(int media) { 
     try { 
      switch (media) { 
       case LOCAL_AUDIO: 
        /** 
        * TODO: Set the path variable to a local audio file path. 
        */ 
        if (path == "") { 
         // Tell the user to provide an audio file URL. 
         Toast 
           .makeText(
             MediaPlayerDemo_Audio.mp, 
             "Please edit MediaPlayer_Audio Activity, " 
               + "and set the path variable to your audio file path." 
               + " Your audio file must be stored on sdcard.", 
             Toast.LENGTH_LONG).show(); 

        } 
        mMediaPlayer = new MediaPlayer(); 
        mMediaPlayer.setLooping(sound_loop); 
        mMediaPlayer.setDataSource(MediaPlayerDemo_Audio.mp, Uri.parse(path)); 
        mMediaPlayer.prepare(); 
        resetTimer(); 
        startTimer(); 
        mMediaPlayer.start(); 

        Intent intent = new Intent(MediaPlayerDemo_Audio.PLAY_START); 
        sendBroadcast(intent); 


        break; 
       case RESOURCES_AUDIO: 
        /** 
        * TODO: Upload a audio file to res/raw folder and provide 
        * its resid in MediaPlayer.create() method. 
        */ 
        // mMediaPlayer = MediaPlayer.create(this, R.raw.test_cbr); 
        //mMediaPlayer.start(); 

      } 
      //tx.setText(path); 

     } catch (Exception e) { 
      //Log.e(TAG, "error: " + e.getMessage(), e); 
     } 

    } 

나는 소리를 재생하는 MediaPlayerDemo_Audio.java을 사용하고 있습니다 : 여기가 문제를 일으키는 생각하는 코드입니다. 위의 코드에서 볼 수 있듯이 mMediaPlayer.setDataSource (MediaPlayerDemo_Audio.mp, Uri.parse (path)); 파일과 미디어 플레이어를 검색하는 코드를 호출하고 있다고 생각합니다. 나는 아직 안드로이드 코드에 익숙하지 않다.

public class MediaPlayerDemo_Audio extends Activity { 

public static String path; 
private String fname; 

private static Intent PlayerIntent; 
public static String STOPED = "stoped"; 
public static String PLAY_START = "play_start"; 
public static String PAUSED = "paused"; 
public static String UPDATE_SEEKBAR = "update_seekbar"; 

public static boolean is_loop = false; 
private static final int LOCAL_AUDIO=0; 


private Button play_pause, stop; 
private SeekBar seek_bar; 

public static MediaPlayerDemo_Audio mp; 
private AudioPlayerService mPlayerService; 



@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.media_player_layout); 
    //getWindow().setTitle("SoundPlayer"); 
    //getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, 
     // android.R.drawable.ic_media_play); 

    play_pause = (Button)findViewById(R.id.play_pause); 
    play_pause.setOnClickListener(play_pause_clk); 
    stop = (Button)findViewById(R.id.stop); 
    stop.setOnClickListener(stop_clk); 
    seek_bar = (SeekBar)findViewById(R.id.seekBar1); 
    seek_bar.setMax(100); 
    seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
     public void onStopTrackingTouch(SeekBar seekBar) {    
      if (mPlayerService!=null) { 
       int seek_pos = (int) ((double)mPlayerService.getduration()*seekBar.getProgress()/100); 
       mPlayerService.seek(seek_pos);     
      } 
     } 
     public void onStartTrackingTouch(SeekBar seekBar) { 
     } 

     public void onProgressChanged(SeekBar seekBar, int progress, 
       boolean fromUser) { 


     } 
    }); 

    play_pause.setText("Pause"); 
    stop.setText("Stop");   

    //int idx = path.lastIndexOf("/"); 
    //fname = path.substring(idx+1); 
    //tx.setText(path); 


    IntentFilter filter = new IntentFilter(); 
    filter.addAction(STOPED); 
    filter.addAction(PAUSED); 
    filter.addAction(PLAY_START); 
    filter.addAction(UPDATE_SEEKBAR); 

    registerReceiver(mPlayerReceiver, filter); 

    PlayerIntent = new Intent(MediaPlayerDemo_Audio.this, AudioPlayerService.class); 
    if (AudioPlayerService.path=="") AudioPlayerService.path=path; 
    AudioPlayerService.sound_loop = is_loop; 
    startService(PlayerIntent); 
    bindService(PlayerIntent, mConnection, 0); 
    if (mPlayerService!=null && mPlayerService.is_pause==true) play_pause.setText("Play"); 
    mp = MediaPlayerDemo_Audio.this; 

} 

private BroadcastReceiver mPlayerReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String act = intent.getAction(); 
     if (act.equalsIgnoreCase(UPDATE_SEEKBAR)){ 
      int val = intent.getIntExtra("seek_pos", 0); 
      seek_bar.setProgress(val); 
      TextView counter = (TextView) findViewById(R.id.time_view); 
      counter.setText(DateUtils.formatElapsedTime((long) (intent.getLongExtra("time", 0)/16.666))); 
      //tx.setText(fname); 
     } 
     else if (act.equalsIgnoreCase(STOPED)) { 
      play_pause.setText("Play");    
      seek_bar.setProgress(0); 
      stopService(PlayerIntent); 
      unbindService(mConnection); 
      mPlayerService = null; 
      TextView counter = (TextView) findViewById(R.id.time_view); 
      counter.setText(DateUtils.formatElapsedTime(0)); 
     } 
     else if (act.equalsIgnoreCase(PLAY_START)){ 
      play_pause.setText("Pause"); 
     } 
     else if (act.equalsIgnoreCase(PAUSED)){ 
      play_pause.setText("Play"); 
     } 

    } 
}; 
private ServiceConnection mConnection = new ServiceConnection() { 
    @Override 
    public void onServiceConnected(ComponentName arg0, IBinder arg1) { 
     mPlayerService = ((AudioPlayerService.LocalBinder)arg1).getService(); 
     if (mPlayerService.is_pause==true) { 
      play_pause.setText("Play"); 
      seek_bar.setProgress(mPlayerService.seek_pos); 
     } 
     if (mPlayerService.mTime!=0) { 
      TextView counter = (TextView) findViewById(R.id.time_view); 
      counter.setText(DateUtils.formatElapsedTime((long) (mPlayerService.mTime/16.666))); 
     } 
     if (path.equalsIgnoreCase(AudioPlayerService.path)==false){ 
      AudioPlayerService.path = path; 
      mPlayerService.restart(); 
     } 
    } 

    @Override 
    public void onServiceDisconnected(ComponentName arg0) { 
     // TODO Auto-generated method stub 
     mPlayerService = null; 
    } 
}; 
private OnClickListener play_pause_clk = new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     if (mPlayerService!=null) 
      mPlayerService.play_pause(); 
     else{ 
      AudioPlayerService.path=path; 
      startService(PlayerIntent); 
      bindService(PlayerIntent, mConnection, 0); 
     } 
    } 

}; 
private OnClickListener stop_clk = new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     if (mPlayerService==null) return; 
     mPlayerService.Stop();   
    } 

}; 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    // TODO Auto-generated method stub 


} 

}

파일이 특수 문자를 사용한 구문 분석 및 MediaPlayer를 제대로 재생할 수 있도록 내가이 문제를 해결할 수있는 방법은 다음과 같습니다 MediaPlayerDemo_Audio.java의 코드는? 내가 놓친 게 있니?

아마 내 AudioPlayerService의 전체 코드를 보여 도움이 될 것이다 :

public class AudioPlayerService extends Service { 
public MediaPlayer mMediaPlayer; 
protected long mStart; 
public long mTime; 
public int seek_pos=0; 
public static boolean sound_loop = false; 
public boolean is_play, is_pause; 
public static String path=""; 
private static final int LOCAL_AUDIO=0; 
private static final int RESOURCES_AUDIO=1;  

private int not_icon; 
private Notification notification; 
private NotificationManager nm; 
private PendingIntent pendingIntent; 
private Intent intent; 

@SuppressWarnings("deprecation") 
@Override 
public void onCreate() { 
    playAudio(LOCAL_AUDIO); 
    mTime=0; 

    is_play = true; 
    is_pause = false; 

    CharSequence ticker = "Touch to return to app"; 
    long now = System.currentTimeMillis(); 
    not_icon = R.drawable.play_notification; 
    notification = new Notification(not_icon, ticker, now); 

    nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

    Context context = getApplicationContext(); 
    intent = new Intent(this, MediaPlayerDemo_Audio.class); 
    pendingIntent = PendingIntent.getActivity(context, 0,intent, 0); 


    PhoneStateListener phoneStateListener = new PhoneStateListener() { 
     @Override 
     public void onCallStateChanged(int state, String incomingNumber) { 
      if (state == TelephonyManager.CALL_STATE_RINGING) { 
       //INCOMING call 
       //do all necessary action to pause the audio 
       if (is_play==true && is_pause==false){ 
        play_pause(); 
       } 

      } else if(state == TelephonyManager.CALL_STATE_IDLE) { 
       //Not IN CALL 
       //do anything if the phone-state is idle 
      } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) { 
       //A call is dialing, active or on hold 
       //do all necessary action to pause the audio 
       //do something here 
       if (is_play==true && is_pause==false){ 
        play_pause(); 
       } 
      } 
      super.onCallStateChanged(state, incomingNumber); 
     } 
    };//end PhoneStateListener 

    TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); 
    if(mgr != null) { 
     mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); 
    } 

    OnAudioFocusChangeListener myaudiochangelistener = new OnAudioFocusChangeListener(){ 

     @Override 
     public void onAudioFocusChange(int arg0) { 
      //if (arg0 ==AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK){ 
       if (is_play==true && is_pause==false){ 
        play_pause(); 
       } 
      //} 

     } 

    }; 

    AudioManager amr = (AudioManager)getSystemService(AUDIO_SERVICE); 
    amr.requestAudioFocus(myaudiochangelistener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); 
} 
@SuppressWarnings("deprecation") 
@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Context context = getApplicationContext(); 
    String title = "VoiceRecorder App"; 
    CharSequence message = "Playing.."; 
    notification.setLatestEventInfo(context, title, message,pendingIntent); 
    nm.notify(101, notification); 
    return START_STICKY; 
} 
public class LocalBinder extends Binder { 
    AudioPlayerService getService() { 
     return AudioPlayerService.this; 
    } 
} 
public void restart(){ 
    mMediaPlayer.stop(); 
    playAudio(LOCAL_AUDIO); 
    mTime=0; 

    is_play = true; 
    is_pause = false; 
} 
@Override 
public IBinder onBind(Intent arg0) { 

    return mBinder; 
} 
private final IBinder mBinder = new LocalBinder(); 

public void Stop() 
{   
    mMediaPlayer.stop(); 
    Intent intent1 = new Intent(MediaPlayerDemo_Audio.STOPED); 
    sendBroadcast(intent1); 

    resetTimer(); 

    is_play=false; 
    is_pause=false; 
} 
public void play_pause() 
{ 
    if (is_play==false){ 
     try { 
      mMediaPlayer.start();   
      startTimer();  

      is_play=true; 
      is_pause = false; 
     } catch (Exception e) { 
      //Log.e(TAG, "error: " + e.getMessage(), e); 
     } 

     Intent intent = new Intent(MediaPlayerDemo_Audio.PLAY_START); 
     sendBroadcast(intent); 
    } 
    else{ 
     mMediaPlayer.pause(); 
     stopTimer(); 
     Intent intent1 = new Intent(MediaPlayerDemo_Audio.PAUSED); 
     sendBroadcast(intent1); 

     is_play = false; 
     is_pause = true; 
    } 

} 
public int getduration() 
{ 
    return mMediaPlayer.getDuration(); 
} 

public void seek(int seek_pos) 
{ 
    mMediaPlayer.seekTo(seek_pos); 
    mTime = seek_pos; 
} 
    public void playAudio(int media) { 
     try { 
      switch (media) { 
       case LOCAL_AUDIO: 
        /** 
        * TODO: Set the path variable to a local audio file path. 
        */ 
        if (path == "") { 
         // Tell the user to provide an audio file URL. 
         Toast 
           .makeText(
             MediaPlayerDemo_Audio.mp, 
             "Please edit MediaPlayer_Audio Activity, " 
               + "and set the path variable to your audio file path." 
               + " Your audio file must be stored on sdcard.", 
             Toast.LENGTH_LONG).show(); 

        } 
        mMediaPlayer = new MediaPlayer(); 
        mMediaPlayer.setLooping(sound_loop); 
        mMediaPlayer.setDataSource(MediaPlayerDemo_Audio.mp, Uri.parse(path)); 
        mMediaPlayer.prepare(); 
        resetTimer(); 
        startTimer(); 
        mMediaPlayer.start(); 

        Intent intent = new Intent(MediaPlayerDemo_Audio.PLAY_START); 
        sendBroadcast(intent); 


        break; 
       case RESOURCES_AUDIO: 
        /** 
        * TODO: Upload a audio file to res/raw folder and provide 
        * its resid in MediaPlayer.create() method. 
        */ 
        // mMediaPlayer = MediaPlayer.create(this, R.raw.test_cbr); 
        //mMediaPlayer.start(); 

      } 
      //tx.setText(path); 

     } catch (Exception e) { 
      //Log.e(TAG, "error: " + e.getMessage(), e); 
     } 

    } 

    @SuppressLint("HandlerLeak") 
    private Handler mHandler = new Handler() { 
     public void handleMessage(Message msg) { 
      long curTime = System.currentTimeMillis(); 
      mTime += curTime-mStart; 
      mStart = curTime; 

      int pos = (int) ((double)mMediaPlayer.getCurrentPosition()*100/mMediaPlayer.getDuration()); 
      seek_pos = pos; 
      Intent intent1 = new Intent(MediaPlayerDemo_Audio.UPDATE_SEEKBAR); 
      if (mMediaPlayer.isLooping()) intent1.putExtra("time", (long)mMediaPlayer.getCurrentPosition()); 
      else intent1.putExtra("time", mTime); 
      intent1.putExtra("seek_pos", pos);  
      sendBroadcast(intent1); 

      if (mMediaPlayer.isPlaying()==false && mMediaPlayer.isLooping()==false){     
       mMediaPlayer.stop(); 
       resetTimer(); 
       Intent intent2 = new Intent(MediaPlayerDemo_Audio.STOPED); 
       sendBroadcast(intent2); 
       is_play=false; 
      }   
      if (mTime > 0) mHandler.sendEmptyMessageDelayed(0, 10); 
     }; 
    }; 
    private void startTimer() { 
     mStart = System.currentTimeMillis(); 
     mHandler.removeMessages(0); 
     mHandler.sendEmptyMessage(0); 

    } 

    private void stopTimer() { 
     mHandler.removeMessages(0); 

    } 

    private void resetTimer() { 
     stopTimer(); 
     mTime = 0; 
    } 


    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     // TODO Auto-generated method stub 
     if (mMediaPlayer != null) { 
      mMediaPlayer.stop(); 
      mMediaPlayer.release(); 
      mMediaPlayer = null; 
     } 
     nm.cancel(101); 
    } 

} 로컬 파일에 대한

답변

4

Uri.parse()를 사용하지 마십시오. Uri.fromFile()을 사용하여 해당 파일을 가리키는 File 개체를 전달하십시오. 이것은 #과 같은 특수 문자를 올바르게 이스케이프해야합니다.

+0

이렇게했습니다. 나는 그 해결책을 또 다른 포스트에서 보았고 그것을 구현하려고 시도했었다. 그러나 나는 그것을 올바르게 이전에하지 말았어야한다고 생각한다. 감사! –

+0

제안에 따라 Uri.parse의 모든 인스턴스를 Uri.fromFile로 변경했지만 이제는 앱에 포함 된 내 사운드 파일이 재생되지 않습니다. Uri.fromFile은 "#"과 같은 특수 문자로 녹음 된 녹음 된 사운드를 재생합니다 (음성 녹음기 앱 있음). 그러나 이미 앱에 번들링 된 사운드는 재생되지 않습니다. Uri.parse는 파일 이름에 "#"과 같은 특수 문자가있는 것을 제외하고는 앱에서 모든 소리를 재생합니다. Uri.parse가 호출 된 모든 인스턴스에 로직을 추가하려고 시도했지만 모든 인스턴스가 전화가 아닌 앱의 사운드 인 경우 if 문을 사용할 수있는 것은 아닙니다. –

+0

@NickLopez : "앱에서 번들링 된 사운드를 재생하지 못합니다"- 기술적 인 관점에서 볼 때 번들링 된 사운드가 무엇인지는 알지 못합니다. 원시 자원입니까? 자산? 처음 실행했을 때 다운로드 한 파일은 무엇입니까? 무작위로 생성 한 파일은 무엇입니까? 다른 것? – CommonsWare

관련 문제