将声音录制到android Wear上的文件中,并使用WearCompanionLibrary将其发送到智能手机

时间:2017-05-31 00:24:56

标签: java android wear-os file-transfer wear-companion-library

我正在创建一个应用程序,允许用户使用智能手表录制语音,然后使用Wear Companion Library将其发送到智能手机。我正在录制文件,我认为发送它也有效(代码运行),但智能手机没有收到任何内容。

这是我的代码:

磨损

public class MainActivity extends Activity implements WearFileTransfer.OnChannelTransferProgressListener {
private static final String TAG = "MainActivity";
private static final int RECORDING_REQUEST_CODE = 1;
private static final int PERMISSIONS_REQUEST_CODE = 2;
public static final String EXTRA_RECORDING_FILE_NAME
        = "com.google.devrel.wcl.widgets.wclrecorderactivity.EXTRA_RECORDING_FILE_NAME";
public static final String EXTRA_RECORDING_STATUS
        = "com.google.devrel.wcl.widgets.wclrecorderactivity.EXTRA_RECORDING_STATUS";
public static final String CAPABILITY_FILE_PROCESSOR = "file_processor";

ImageButton microphoneButton;
ImageButton messageButton;

private ProgressBar mProgressBar;
private Handler mHandler;

private WearManager mWearManager;
private AbstractWearConsumer mWearConsumer;

boolean recording; //indicates if recording should stop or continue

public void record(View view) {
    Intent intent = new Intent(this, WclRecorderActivity.class);
    intent.putExtra(WclRecorderActivity.EXTRA_RECORDING_FILE_NAME, fileName());
    startActivityForResult(intent, RECORDING_REQUEST_CODE);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WearManager.initialize(getApplicationContext());
    setContentView(R.layout.activity_main);
    mWearManager = WearManager.getInstance();

    // we define a listener to inform us of the status of the file transfer
    mWearConsumer = new AbstractWearConsumer() {
        @Override
        public void onWearableSendFileResult(int statusCode, String requestId) {
            Log.d(TAG, String.format("Status Code=%d, requestId=%s", statusCode, requestId));
        }
    };

    mWearManager.addWearConsumer(mWearConsumer);

    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            microphoneButton = (ImageButton) findViewById(R.id.microphoneButton);
            messageButton = (ImageButton) findViewById(R.id.messageButton);
            mProgressBar = (ProgressBar) findViewById(R.id.progressBar);

        }
    });

    checkPermissions();
    mHandler = new Handler();
}

private void checkPermissions() {
    boolean recordAudioPermissionGranted =
            ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
                    == PackageManager.PERMISSION_GRANTED;

    if (recordAudioPermissionGranted) {
        //            microphoneButton.setEnabled(true);
    } else {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO},
                PERMISSIONS_REQUEST_CODE);
    }

}

@Override
public void onProgressUpdated(final long progress, final long total) {
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            mProgressBar.setMax((int) total);
            mProgressBar.setProgress((int) progress);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == RECORDING_REQUEST_CODE) {
            int recordingStatus = data.getIntExtra(EXTRA_RECORDING_STATUS, 1);
            if (recordingStatus == 0) {
                String filename = data.getStringExtra(EXTRA_RECORDING_FILE_NAME);
                Log.i("filename", filename);
                sendFile(filename);

            }
        }
    }
}

public void sendFile(String targetName) {
    Set<Node> nodes = mWearManager.getConnectedNodes();

    if (nodes != null && !nodes.isEmpty()) {
        Node targetNode = nodes.iterator().next();
        Log.d(TAG, "Targeting node: " + targetNode);
        File file = new File(getApplicationContext().getFilesDir() + "/" + targetName);
        Log.i("targetname", targetName);
        WearFileTransfer fileTransfer = new WearFileTransfer.Builder(targetNode)
                .setTargetName(targetName)
                .setFile(file)
                .build();
        fileTransfer.startTransfer();
    }
}

String fileName() {
    DateFormat df = new SimpleDateFormat("yyMMddHHmmss");
    String date = df.format(Calendar.getInstance().getTime());
    String name = "audio_message_" + date + ".mp3";
    return name;
}

电话

public class MainActivity extends AppCompatActivity {
private static final String TAG = "FileTransferFragment";
private WearManager mWearManager;
private WearConsumer mWearConsumer;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WearManager.initialize(getApplicationContext());
    setContentView(R.layout.activity_main);

    setUpWearListeners();

    // register our listeners
    mWearManager.addWearConsumer(mWearConsumer);

}

private void setUpWearListeners() {
    mWearManager = WearManager.getInstance();
    mWearConsumer = new AbstractWearConsumer() {

        @Override
        public void onWearableFileReceivedResult(int statusCode, String requestId,
                                                 File savedFile, String originalName) {
            Log.d(TAG, String.format(
                    "File Received: status=%d, requestId=%s, savedLocation=%s, originalName=%s",
                    statusCode, requestId, savedFile.getAbsolutePath(), originalName));

        }

    };
}

非常感谢!

0 个答案:

没有答案