[英]How do I stream music from a file inside encrypted zip without decompressing the entire file?
我必須播放音頻文件。 這是在zip文件中,這是sdcard中的現有文件。 並且音頻文件已加密。 因此,在解密音頻時,我將獲得輸入流中的數據。
我不想解壓縮因為它占用磁盤上的空間。
正如我調查的那樣,如果我有流,我沒有直接播放音頻的線索。 它唯一可能通過網絡。 在這種情況下不是這樣的。
所以我想的是產生一個線程,它會不斷地將數據(字節)附加到文件中。 當這開始時,我打電話給MediadPlayer
開始工作。
媒體播放器沒問題。 樂趣從這里開始:假設音頻文件在6分鍾 - 5MB。 緩沖可能發生在2MB。 在搜索欄中,我可以看到2分鍾作為我的最大持續時間。 這是完全正確的。 當緩沖仍然繼續發生時,我想更新搜索條中的時間,其長度(搜索條長度)與給定時間成正比。 我該怎么做呢
我為此嘗試了OnBufffering,它沒有用。 我猜它實際上是流媒體音頻文件,如果它通過網絡播放。
請給我一些簡單的解決方案,如何完成這項工作? 不要讓我覆蓋MediaPlayer
類並對其進行處理。
任何幫助表示贊賞。 如果您需要更清楚,請告訴我。
public class NotesAudDisplay extends Activity implements OnPreparedListener, MediaController.MediaPlayerControl{
private static final String TAG = "activity-NotesAudioDisplay";
private String audioFilePath;
private String notesFileName;
private String mcfFileName;
private String key;
private SeekBar seekBarProgress;
private NotesElement notesElement = null;
private String notesTittle = "", notesHeading = "";
private TextView heading_tv, playerStatus_tv;
private QuesBuilder qb = null;
private MediaPlayer mediaPlayer = null;
private MediaController mediaController;
private Drawable play_butt, pause_butt;
private ProgressDialog pd;
private Resources res = null;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_notesdisplay);
res = getResources();
play_butt = res.getDrawable(R.drawable.play);
pause_butt = res.getDrawable(R.drawable.pause);
heading_tv = (TextView) findViewById(R.id.notesHeading_tv);
playerStatus_tv = (TextView) findViewById(R.id.playerStatus_tv);
Intent intent = getIntent();
notesTittle = intent.getStringExtra("notesTittle");
notesFileName = intent.getStringExtra("notesFileName");
mcfFileName = intent.getStringExtra("mcfFileName");
key = intent.getStringExtra("key");
TextView tittle_tv = (TextView) findViewById(R.id.notesTittle_tv);
tittle_tv.setText(notesTittle);
NotesXMLParser nxp = new NotesXMLParser(this, notesFileName,
mcfFileName, key);
nxp.OpenXmlDocument();
notesElement = nxp.getNotesContent();
Log.d("TAG", "notesele:" + notesElement);
if (notesElement != null) {
notesHeading = notesElement.getHeading();
heading_tv.setText(notesHeading);
QuesBuilderSet qbs = notesElement.getNotesStatement();
ArrayList quesBuilder = qbs.getQuesBuilderSet();
if (quesBuilder != null) {
Log.d(TAG, " quesBuilder len:" + quesBuilder.size());
for (int i = 0; i < quesBuilder.size(); i++) {
qb = (QuesBuilder) quesBuilder.get(i);
if (qb.getType() == QuesBuilder.SPEECH) {
Log.d(TAG, " AUDIO");
String file = qb.getQuesSpeech();
File f = createTmpAudioFile(file);
boolean decrypt_result = false;
if (f != null) {
new LongOperation().execute(f);
Log.d(TAG,"****before long operation****");
try {
Log.d(TAG,"****before thread operation****");
Thread.sleep(3000);
Log.d(TAG,"****after thread operation****");
setContent();
} catch (Exception e) {
Log.d("InstructionForm", "Sleep thread fails");
}
Log.d(TAG,"****after catch****");
} else {
heading_tv.setText(notesHeading
+ " Unable to play the audio.");
}
} else {
Log.d(TAG, " other:" + qb.getType());
}
}
}
}
}// onCreate
public void setContent() {
mediaController = new MediaController(NotesAudDisplay.this);
mediaPlayer = new MediaPlayer();
Log.d(TAG,"***GOING TO PREP STATE***");
mediaPlayer.setOnPreparedListener(NotesAudDisplay.this);
Log.d(TAG,"***DONE WITH PREP STATE***");
try {
mediaPlayer.setDataSource(audioFilePath);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();
mediaPlayer.start();
playerStatus_tv.setText("Playing.. . ");
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private File createTmpAudioFile(String file) {
DBAdapter dba = new DBAdapter(NotesAudDisplay.this);
dba.open();
String mobiDataPath = dba.get_mobidata_path();
dba.close();
audioFilePath = mobiDataPath + "/" + file;
Log.d(TAG, "tmp audio filePath:" + audioFilePath);
File f = null;
try {
f = new File(audioFilePath);
return f;
} catch (Exception e) {
f = null;
Log.d(TAG, " exception caught in creating audio file on sdcard");
}
return null;
}
private class LongOperation extends AsyncTask<File, Void, Boolean> {
@Override
protected void onPreExecute() {
// TODO run small wheel
// show_wheel();
}
@Override
protected Boolean doInBackground(File... arg0) {
DecryptZipReader dr = new DecryptZipReader();
File f = arg0[0];
Log.d(TAG, "*********copying start*********");
boolean res = dr.getDecryptFileStream(NotesAudDisplay.this,
qb.getQuesSpeech(), mcfFileName, key, f);
return new Boolean(res);
}
@Override
protected void onPostExecute(Boolean result) {
// close_wheel();
Log.d(TAG, "*********copying stop*********");
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
@Override
protected void onStop() {
super.onStop();
mediaPlayer.stop();
mediaPlayer.release();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//the MediaController will hide after 3 seconds - tap the screen to make it appear again
mediaController.show(0);
return false;
}
//--MediaPlayerControl methods----------------------------------------------------
public void start() {
mediaPlayer.start();
}
public void pause() {
mediaPlayer.pause();
}
public int getDuration() {
Log.d(TAG,"***duration:"+mediaPlayer.getDuration());
return mediaPlayer.getDuration();
}
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
public void seekTo(int i) {
mediaPlayer.seekTo(i);
}
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
public int getBufferPercentage() {
return 0;
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
//--------------------------------------------------------------------------------
public void onPrepared(MediaPlayer mediaPlayer) {
Log.d(TAG, "*********onPrepared*********");
mediaController.setMediaPlayer(this);
mediaController.setAnchorView(findViewById(R.id.main_audio_view));
handler.post(new Runnable() {
public void run() {
mediaController.setEnabled(true);
mediaController.show(0);
}
});
}
}
Afaik,您可以從zip中獲取FileDescriptor,而無需使用Google的ZipResource庫提取,它僅適用於擴展包,但如果您只存儲項目而不壓縮它,它可以正常工作(zip -r -n .mp3: .png:.txt命運之源)。
FileDescriptor已准備好與MediaPlayer一起使用,但如果其加密可能有兩個描述符可能會幫助您優化解密通量。
public ZipResourceFile getExpansionFiles(Context context){
ZipResourceFile expansionFile = null;
try {
expansionFile = new ZipResourceFile(
Environment.getExternalStorageDirectory() + "/MyFolder" + "/" + "/MyFile" + ".zip");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
然后使用:ZipResourceFile z = getExpansionFiles(mContext);
AssetFileDescriptor afd = z.getAssetFileDescriptor(mItem +“。mp3”);
我希望它有所幫助,無論如何,你確定你不想等到文件被完全解密,然后播放它而不用擔心所有這些即時頭痛?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.