gpt4 book ai didi

android - 在不首先打开文件的情况下在 Android 上生成 FileDescriptor

转载 作者:太空狗 更新时间:2023-10-29 15:01:08 26 4
gpt4 key购买 nike

在 Android 中,是否可以直接从字节数组生成 FileDescriptor 而无需先打开文件?

在 Android 2.2 中,我即时生成一个 MIDI 文件,然后使用 MediaPlayer 播放它。我在下面包含了成功执行此操作的 Main.java 文件的文本。到目前为止一切顺利。

但是,这个过程首先调用...

FileOutputStream outputStream = openFileOutput(file, MODE_PRIVATE);
outputStream.write(byteStream);
outputStream.close();

...写出文件,然后调用...

FileInputStream inputStream = new FileInputStream(midifile);
FileDescriptor fileDescriptor = inputStream.getFD();

... 在调用之前读回:

mediaPlayer.setDataSource(fileDescriptor);

在我看来这很浪费。我可以直接从 byteArray 创建 FileDescriptor,以便立即播放 MIDI 流吗?


== 工作代码==

package com.example.midi;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;

public class Main extends Activity {

private String file = "midi.mid";
private MediaPlayer mediaPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

createNewMIDIFile();
playNewMIDIFile();
}

public void createNewMIDIFile() {
Integer[] stream = new Integer[]{
//
0x4d, 0x54, 0x68, 0x64, // MThd = MIDI file designator
0x00, 0x00, 0x00, 0x06, // Standard MIDI File (SMF)
0x00, 0x01, 0x00, 0x02, // multiple-track format: 2 tracks
0x00, 0x40, // 64 ticks per beat (quarter note)
0x4D, 0x54, 0x72, 0x6B, // Header for track 1
0x00, 0x00, 0x00, 0x0B, // 11 bytes to describe the track
0x00, 0xFF, 0x51, 0x03, // set tempo:
0x0F, 0x42, 0x40, // 1,000,000 microseconds / beat: 60 bpm
0x00, 0xFF, 0x2F, 0x00, // End of track 1
0x4D, 0x54, 0x72, 0x6B, // Header for track 2
0x00, 0x00, 0x00, 0x0F, // 15 bytes to describe the track
0x00, // Immediately
0xC1, 0x01, // change instrument for track 2 to piano
0x00, // Immediately
0x91, 0x3C, 0x7F, // play middle C with a velocity of 127
0x30, // 48 ticks later (dotted eighth note)
0x81, 0x3C, 0x00, // stop playing the middle C
0x00, 0xFF, 0x2F, 0x00 // End of track 2
};

int length = stream.length;
byte[] byteStream = new byte[length];
for (int ii = 0; ii < length; ii++) {
byteStream[ii] = (byte) (stream[ii] % 256);
}

try {
FileOutputStream outputStream = openFileOutput(file, MODE_PRIVATE);
outputStream.write(byteStream);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public void play(View view) {
/* Triggered by a button defined in activity_main.xml as
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="play"
android:text="Play MIDI" />
*/
playNewMIDIFile();
}

public void playNewMIDIFile() {
try {
String filename = getFilesDir() + "/" + file;
File midifile = new File(filename);
FileInputStream inputStream = new FileInputStream(midifile);
FileDescriptor fileDescriptor = inputStream.getFD();
mediaPlayer.reset();
mediaPlayer.setDataSource(fileDescriptor);
inputStream.close();
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}

有关即时构建 MIDI 文件的更多信息,请参阅 kevinboone.net , skytopiasonicspot

最佳答案

没有必要模拟FileDescriptor
您可以实现自己的 MediaDataSource并将其设置为调用 MediaPlayer::setDataSource(yourMediaDataSource) .

关于android - 在不首先打开文件的情况下在 Android 上生成 FileDescriptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26681062/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com